<?php
	class Goods_topic extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($title = 0, $tk_category_id = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
				$tk_category_id = !empty($post_data['tk_category_id']) ? $post_data['tk_category_id'] : 0;
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('title' => $title, "tk_category_id" => $tk_category_id);
			$this->load->model("Subjects");
			$result = $this->Subjects->get_subject_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/subject/index/' . $title . '/' . $tk_category_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->load->model("Tk_categorys");
			$category_list = $this->Tk_categorys->get_top_categorys();
			$this->smarty->assign("category_list", $category_list);
			$this->smarty->assign("action", '科目列表');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("subject_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("goods_topic/list.html");
		}

		function add($subject_id = 0){
			$post_data = $this->input->post();
			if($post_data){
				$num = intval($post_data['num']);
				if($post_data['num'] > 0){
					$school_info = get_field_by_where("o2o_school", "agent_id", "id='{$post_data['school_id']}'");
					if(!empty($school_info['agent_id'])){
						$agent_info = get_field_by_where("agents", "points", "id='{$school_info['agent_id']}'");
						if(!empty($agent_info['points'])){
							$post_data['agent_points'] = $agent_info['points'];
							$post_data['commission'] = sprintf("%.2f", ($post_data['num'] * $post_data['price']) * 22 / 100);
						}
					}
					$post_data['agent_id'] = $school_info['agent_id'];
					$post_data['data_id'] = $subject_id;
					$post_data['data_type'] = 4;
					$post_data['type'] = 1;
					$post_data['add_time'] = time();
					$this->db->insert('o2o_school_order_records',$post_data);
					$record_id = $this->db->insert_id();

					$time = time();
					for ($i=0; $i < $num; $i++) { 
						$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
						$code = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
						$code_data[] = array("school_id" => $post_data['school_id'], "subject_id" => $subject_id, "add_time" => $time, "code" => "DV" . $code, "record_id" => $record_id, "expire_day" => $post_data['expire_day']);
					}
					$insert_result = $this->db->insert_batch("o2o_school_system_topic_codes", $code_data);
				}else{
					show_error($_SERVER['HTTP_REFERER'], "激活码数量不能为空！");exit;
				}
				/*if($insert_result){
					$post_data['data_id'] = $subject_id;
					$post_data['type'] = 1;
					$post_data['add_time'] = time();
					$result = $this->db->insert('o2o_school_order_records',$post_data);
				}*/
				if($record_id > 0){
					show_message($_SERVER['HTTP_REFERER'],'添加成功');
				}else{
					show_message($_SERVER['HTTP_REFERER'],'操作失败');
				}
			}else{
				$this->load->model("Schools");
				$school_data = $this->Schools->get_school_list();
				$school_list = $school_data;
				$this->smarty->assign("school", $school_list);
				$this->smarty->assign("action", '授权');
				$this->smarty->display("goods_topic/save.html");
			}
		}
	}